#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/list.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include "common.h"
#include "memcg.h"

#define CHR_NAME "memhunter"
static DEFINE_MUTEX(dev_mutex);
static int memhunter_dev_major = -1;
static struct class *memhunter_dev_class = NULL;
static struct device *memhunter_dev = NULL;

int memhunter_handler_cmd(unsigned int cmd, unsigned long arg)
{
    int ret = -EINVAL;
    int type, nr;
    printk("debug -- 1\n");

    if (!mutex_trylock(&dev_mutex))
        return -EBUSY;

    type = _IOC_TYPE(cmd);
    nr = _IOC_NR(cmd);
    printk("type: %d\n", nr);
    switch (nr) {
        case MEMHUNTER_CACHE_TYPE_FILE:
            ret = filecache_main(arg);
            break;
        case MEMHUNTER_CACHE_TYPE_MEMCG_DYING:
            printk("dying\n");
            ret = memcg_dying_main(arg);
            break;
        case MEMHUNTER_CACHE_TYPE_MEMCG_ONE:
            ret = memcg_one_main(arg);
            break;
        default:
            printk("defualt ioctl cmd =%d, nr = %d\n", type, nr);
            break;
    }

    mutex_unlock(&dev_mutex);
    return ret;
}

static __init int memhunter_init(void)
{
    return 0;
}

static __exit void memhunter_exit(void)
{
    return ;
}

